<template>
  <nav v-if="shouldShowNavbar" class="top-navbar">
    <div class="nav-content">
      <div class="nav-left">
        <h1 class="logo" @click="goHome" style="cursor:pointer;">🎓 计考通</h1>
      </div>
      <div class="nav-center">
        <div class="nav-menu">
          <div class="nav-item" @click="handleGo('/home/learning')">📚 学习练习</div>
          <div class="nav-item" @click="handleGo('/home/ExamPagehome')">📝 考试</div>
          <div class="nav-item" @click="handleGo('/home/user-communication')">💬 社区讨论</div>
          <div class="nav-item" @click="handleGo('/home/wrong-questions')">📖 错题重练</div>          
          <div class="nav-item" @click="handleGo('/home/ai-progress-analysis')">🤖 Bingo</div>
        </div>
      </div>
      <div class="nav-right">
        <div class="user-capsule" @click="handleToggleUserMenu">
          <img :src="user.avatar" alt="头像" class="user-avatar" />
          <span class="username">{{ user.name }}</span>
          <div v-if="showUserMenu" class="user-dropdown" @click.stop>
            <div class="dropdown-item" @click="handleGo('/home/user-center')">
              <i class="icon">👤</i>个人中心
            </div>
            <div class="dropdown-item" @click="handleLogout">
              <i class="icon">🚪</i>退出登录
            </div>
          </div>
        </div>
      </div>
    </div>
  </nav>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../../store/UserStore'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const showUserMenu = ref(false)

// 从 store 获取用户信息
const user = computed(() => ({
  name: userStore.userInfo?.username || '用户',
  avatar: userStore.getAvatar
}))

// 计算属性：判断是否显示导航栏
const shouldShowNavbar = computed(() => {
  return !route.path.startsWith('/home/user-center')
})

const handleGo = (path) => {
  router.push(path)
}

const handleToggleUserMenu = (event) => {
  if (event) {
    event.stopPropagation()
  }
  showUserMenu.value = !showUserMenu.value
}

const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}

const goHome = () => {
  router.push('/home')
}
</script>

<style scoped>
.top-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(107, 155, 210, 0.1);
  z-index: 1000;
  transition: all 0.3s ease;
}

.nav-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 30px;
}

.nav-left {
  display: flex;
  align-items: center;
}

.logo {
  font-size: 1.9rem !important;
  font-weight: bold;
  letter-spacing: 2px;
  color: #4167B1;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.nav-center {
  display: flex;
  justify-content: center;
}

.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-item {
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #4a6fa5;
  font-weight: 500;
}

.nav-item:hover {
  background: rgba(107, 155, 210, 0.1);
  transform: translateY(-2px);
}

.nav-right {
  display: flex;
  align-items: center;
}

.user-capsule {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: rgba(107, 155, 210, 0.1);
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  z-index: 1000;
}

.user-capsule:hover {
  background: rgba(107, 155, 210, 0.2);
  transform: translateY(-1px);
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.username {
  font-weight: 500;
  color: #4a6fa5;
}

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 5px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(107, 155, 210, 0.2);
  overflow: hidden;
  min-width: 150px;
  z-index: 1001;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.dropdown-item:hover {
  background-color: rgba(107, 155, 210, 0.1);
}

@media (max-width: 768px) {
  .nav-content {
    padding: 0 15px;
  }
  
  .nav-menu {
    gap: 15px;
  }
  
  .nav-item {
    padding: 6px 12px;
    font-size: 14px;
  }
}
</style>